package com.chenyun.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenyun.sys.common.DataGridView;
import com.chenyun.sys.common.ResultObj;
import com.chenyun.sys.domain.Loginfo;
import com.chenyun.sys.service.impl.LoginfoServiceImpl;
import com.chenyun.sys.vo.LoginfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

@RestController
@RequestMapping("/loginfo")
public class LoginfoController{

    @Autowired
    LoginfoServiceImpl loginfoService;

    // 全查询
    @RequestMapping("/loadAllLoginfo")
    public DataGridView loadAllLoginfo(LoginfoVo loginfoVo){

        // page里面会保持页数，数据等很多信息
        Page<Loginfo> page = new Page<>(loginfoVo.getPage(), loginfoVo.getLimit());

        QueryWrapper<Loginfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNoneBlank(loginfoVo.getLoginname()),"loginname",loginfoVo.getLoginname())
                    .like(StringUtils.isNoneBlank(loginfoVo.getLoginip()),"loginip",loginfoVo.getLoginip())
                    .ge(loginfoVo.getStartTime()!=null, "logintime", loginfoVo.getStartTime())  //大于等于
                    .le(loginfoVo.getEndTime()!=null, "logintime", loginfoVo.getEndTime())     //小于等于
                    .orderByDesc("logintime");  //降序


        loginfoService.page(page,queryWrapper);
        // 将记录数  和记录数据返回
        return new DataGridView(page.getTotal(), page.getRecords());
    }


    /**
     * 删除单条数据
     */
    @RequestMapping("/deleteLoginfo")
    public ResultObj deleteLoginfo(Integer id){
        try {
            loginfoService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


    /**
     * 批量删除数据
     */
    @RequestMapping("/batchDeleteLoginfo")
    public ResultObj batchDeleteLoginfo(LoginfoVo loginfoVo){
        try {
            Collection<Serializable> idList=new ArrayList<Serializable>();
            for (Integer id : loginfoVo.getIds()) {
                idList.add(id);
            }
            this.loginfoService.removeByIds(idList);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }




}
